
//import java.util.Date;
//import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
//import javax.microedition.lcdui.Image;
//import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author VO CHI DUNG
 */
public class PlanTable extends MIDlet implements CommandListener {

    DataTask dataTask = new DataTask("dbTask");

    public PlanTable() throws RecordStoreException {
        display = Display.getDisplay(this);

        addWork = new AddWork(this);
        editWork = new EditWork(this);
        config = new Configuration(this);
        listWork = new ListWork(this);
        menu = new Menu(this);
        alertDialog = new AlertDialog(this);

        dataTask.open();
        listWork.loadListWork(dataTask);

        firstTime = true;
    }

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    protected void startApp() {
        if (firstTime) {
            display.setCurrent(listWork.GetForm());
            firstTime = false;
        }
    }

    public void commandAction(Command c, Displayable d) {

        if (d.equals(listWork.GetForm()))
        {
            listWork.HandleInput(c);
        }

        else if (d.equals(addWork.GetForm()))
        {
            addWork.HandleInput(c);
        }

        else if (d.equals(editWork.GetForm()))
        {
            editWork.HandleInput(c);
        }

        else if (d.equals(config.GetForm()))
        {
            config.HandleInput(c);
        }

        else if(d.equals(menu.GetForm()))
        {
            menu.HandleInput(c);
        }
    }

    public void Exit()
    {
         destroyApp(false);
         notifyDestroyed();
    }

    public void Switch(int iDisplay)
    {
        switch(iDisplay)
        {
            case 0:
                display.setCurrent(listWork.GetForm());
                break;
            case 1:
                addWork.reset();
                display.setCurrent(addWork.GetForm());
                break;
            case 2:
                 display.setCurrent(editWork.GetForm());
                break;
            case 3:
                 display.setCurrent(config.GetForm());
                break;
            case 4:
                 display.setCurrent(menu.GetForm());
                break;
        }
    }

    public void updateListWork()
    {
        listWork.loadListWork(dataTask);
    }

    public void DeleteByID(int id)
    {
        dataTask.delete(id);
        updateListWork();
        GlobalVariableStatic.SelectedWork = -1;
    }

     public void DeleteALL()
    {
        dataTask.deleteAll();
        updateListWork();
        GlobalVariableStatic.SelectedWork = -1;
    }

    public static void showDialog(Displayable displayable)
    {
        alertDialog.show(displayable);
    }

    public Display getDisplay()
    {
        return display;
    }

    public ListWork getListWork()
    {
        return listWork;
    }

    public boolean  loadEditWork()
    {
        RecordTask[] arrRecordTask = dataTask.selectRecordTasks();
        if(arrRecordTask.length <= 0)
            return false;

        if(arrRecordTask[GlobalVariableStatic.SelectedWork] != null)
        {
            editWork.loadEditWork(arrRecordTask[GlobalVariableStatic.SelectedWork]);
            return true;
        }
        return false;
    }

    private Display display;
    private boolean firstTime;
    private ListWork listWork;
    private AddWork addWork;
    private EditWork editWork;
    private Configuration config;
    private Menu menu;
    private static AlertDialog alertDialog;
}
